package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.AccessDoorDeployMapper;
import com.zhijian.warehouse.manage.entity.po.AccessDoorDeploy;
import com.zhijian.warehouse.manage.entity.vo.request.AccessDoorDeploySaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.AccessDoorDeploySearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.AccessDoorDeployVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.enums.DoorEnum;
import com.zhijian.warehouse.manage.service.AccessDoorDeployService;
import com.zhijian.warehouse.manage.service.AccessOptService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 通道门设置业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-11-03 15:31:18
 */
@Slf4j
@Service
public class AccessDoorDeployServiceImpl implements AccessDoorDeployService {

    @Autowired
    private AccessDoorDeployMapper mapper;

    @Override
    public AccessDoorDeploy selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AccessDoorDeployVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        AccessDoorDeploy accessDoorDeploy = this.selectById(id);
        AssertCommonUtil.notNull(accessDoorDeploy, "此数据不存在或者已被删除");
        this.updateByPrimaryKeySelective(AccessDoorDeploy.builder().id(id)
                .isDelete(DelFlagEnum.DELETED.value).build());
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        for (Long id : idList) {
            this.deleteById(id);
        }
    }

    @Override
    public Long insert(AccessDoorDeploy accessDoorDeploy) {
        Date now = new Date();
        accessDoorDeploy.setCreateTime(now);
        accessDoorDeploy.setUpdateTime(now);
        return mapper.insert(accessDoorDeploy) > 0 ? accessDoorDeploy.getId() : 0L;
    }

    @Override
    public Long insertSelective(AccessDoorDeploy accessDoorDeploy) {
        Date now = new Date();
        accessDoorDeploy.setCreateTime(now);
        accessDoorDeploy.setUpdateTime(now);
        return mapper.insertSelective(accessDoorDeploy) > 0 ? accessDoorDeploy.getId() : 0L;
    }

    @Override
    public boolean update(AccessDoorDeploy accessDoorDeploy) {
        Date now = new Date();
        accessDoorDeploy.setUpdateTime(now);
        return mapper.updateByPrimaryKey(accessDoorDeploy) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AccessDoorDeploy record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AccessDoorDeploySaveOrUpdateVo saveOrUpdateVo) {
        AccessDoorDeploy accessDoorDeploy = new AccessDoorDeploy();
        BeanUtils.copyProperties(saveOrUpdateVo, accessDoorDeploy);
        String ipAddr = saveOrUpdateVo.getIpAddr();
        //校验ip加端口格式
        AssertCommonUtil.check(!checkIpPort(ipAddr), "IP地址格式不正确 格式为 ip:port 例如 192.168.1.1:8160");
        Integer count = this.count(AccessDoorDeploySearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .isDelete(DelFlagEnum.USING.value)
                .ipAddr(ipAddr).build());
        AssertCommonUtil.check(count > 0, String.format("IP地址 %s 已存在", ipAddr));
        accessDoorDeploy.setStatus(DoorEnum.CONNECT_TYPE.OFF.value);
        return this.insertSelective(accessDoorDeploy) != 0;
    }

    //校验ip:port格式
    private boolean checkIpPort(String ipAddr) {
        String[] ipPort = ipAddr.split(":");
        if (ipPort.length != 2) {
            return false;
        }
        try {
            int port = Integer.parseInt(ipPort[1]);
            if (port < 1 || port > 65535) {
                return false;
            }
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean modify(AccessDoorDeploySaveOrUpdateVo saveOrUpdateVo) {
        AccessDoorDeploy old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        AccessDoorDeploy accessDoorDeploy = new AccessDoorDeploy();
        BeanUtils.copyProperties(saveOrUpdateVo, accessDoorDeploy);
        String ipAddr = saveOrUpdateVo.getIpAddr();
        AssertCommonUtil.check(!checkIpPort(ipAddr), "IP地址格式不正确 格式为 ip:port 例如 192.168.1.1:8160");
        Integer count = this.count(AccessDoorDeploySearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .isDelete(DelFlagEnum.USING.value)
                .ipAddr(ipAddr).build());
        AssertCommonUtil.check(count > 0, String.format("IP地址 %s 已存在", ipAddr));
        accessDoorDeploy.setIsDelete(DelFlagEnum.USING.value);
        accessDoorDeploy.setStatus(old.getStatus());
        accessDoorDeploy.setCreateUserId(old.getCreateUserId());
        accessDoorDeploy.setCreateTime(old.getCreateTime());
        accessDoorDeploy.setUpdateTime(old.getUpdateTime());
        return this.update(accessDoorDeploy);
    }

    @Override
    public AccessDoorDeployVo selectOne(AccessDoorDeploySearchVo accessDoorDeploySearchVo) {
        return mapper.selectOne(accessDoorDeploySearchVo);
    }

    @Override
    public Integer count(AccessDoorDeploySearchVo accessDoorDeploySearchVo) {
        return mapper.count(accessDoorDeploySearchVo);
    }

    @Override
    public List<AccessDoorDeployVo> selectList(AccessDoorDeploySearchVo accessDoorDeploySearchVo) {
        return mapper.selectList(accessDoorDeploySearchVo);
    }

    @Override
    public Page<AccessDoorDeployVo> selectPage(AccessDoorDeploySearchVo accessDoorDeploySearchVo) {
        AssertCommonUtil.check(Objects.isNull(accessDoorDeploySearchVo.getPageNum()) || Objects.isNull(accessDoorDeploySearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(accessDoorDeploySearchVo);
        //分页
        Page<AccessDoorDeployVo> page = new Page<>(true, count, accessDoorDeploySearchVo.getPageNum(), accessDoorDeploySearchVo.getPageSize());
        accessDoorDeploySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AccessDoorDeployVo> responseVos = this.selectList(accessDoorDeploySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AccessDoorDeploy> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }
}
